package com.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ydd.constant.SysDictTypeConstant;
import com.ydd.context.SysBaseContext;
import com.ydd.entity.*;
import com.ydd.mapper.*;
import com.ydd.result.Result;
import com.ydd.service.LoginService;
import com.ydd.vo.BaseInfoVo;
import com.ydd.vo.UserDataVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired(required = false)
    SysUserMapper sysUserMapper;

    @Autowired(required = false)
    SysRoleMapper sysRoleMapper;

    @Autowired(required = false)
    SysMenuMapper sysMenuMapper;

    @Autowired(required = false)
    SysDictMapper sysDictMapper;

    @Autowired(required = false)
    YddUserMapper yddUserMapper;

    @Autowired(required = false)
    YddArticleMapper yddArticleMapper;

    @Autowired(required = false)
    YddAdvposMapper yddAdvposMapper;

    @Autowired(required = false)
    YddAdvimgMapper yddAdvimgMapper;

    @Autowired(required = false)
    YddTagMapper yddTagMapper;

    @Autowired(required = false)
    YddCateMapper yddCateMapper;


    /**
     * 获取用户资料
     *
     * @return
     */
    @Override
    public Result profile() {

        //获取用户id
        String userId = SysBaseContext.getCurrentId();
        //查询数据封装
        //封装userInfo
        userInfo userInfo = new userInfo();
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        SysUser sysUser = sysUserMapper.selectById(userId);
        //拷贝数据
        BeanUtils.copyProperties(sysUser, userInfo);
        //补全rolename
        QueryWrapper<SysRole> Wrapper = new QueryWrapper<>();

        //处理id不同问题
        String roleId = sysUser.getRoleId();
        String result = roleId.substring(2, roleId.length() - 2);

        SysRole sysRole = sysRoleMapper.selectById(result);
        userInfo.setRolename(sysRole.getName());

        LambdaQueryWrapper<SysMenu> sysMenuLambdaQueryWrapper = Wrappers.lambdaQuery();
        sysMenuLambdaQueryWrapper.eq(SysMenu::getType,1);

        //封装menuList
        List<SysMenu> menuList = sysMenuMapper.selectList(sysMenuLambdaQueryWrapper);


        //封装dictsList
        QueryWrapper<SysDict> dictQueryWrapper = new QueryWrapper<>();
        dictQueryWrapper.eq("type", SysDictTypeConstant.TABLE_TYPE);
        List<SysDict> tableTypeSysDicts = sysDictMapper.selectList(dictQueryWrapper);
        // 清除之前设置的查询条件
        dictQueryWrapper.clear();
        dictQueryWrapper.eq("type", SysDictTypeConstant.INTERFACE_TYPE);
        List<SysDict> interfaceTypeSysDicts = sysDictMapper.selectList(dictQueryWrapper);
        // 清除之前设置的查询条件
        dictQueryWrapper.clear();
        dictQueryWrapper.eq("type", SysDictTypeConstant.REPORT_TYPE);
        List<SysDict> reportTypeSysDicts = sysDictMapper.selectList(dictQueryWrapper);
        // 清除之前设置的查询条件
        dictQueryWrapper.clear();
        dictQueryWrapper.eq("type", SysDictTypeConstant.THEME);
        List<SysDict> themeSysDicts = sysDictMapper.selectList(dictQueryWrapper);
        // 清除之前设置的查询条件
        dictQueryWrapper.clear();
        dictQueryWrapper.eq("type", SysDictTypeConstant.ORDER_TYPE);
        List<SysDict> orderTypeSysDicts = sysDictMapper.selectList(dictQueryWrapper);

        //查询结果以键值对的方式封装进Map中
        Map table_type = new HashMap<>();
        for (SysDict tableTypeSysDict : tableTypeSysDicts) {
            table_type.put(tableTypeSysDict.getValue(), tableTypeSysDict.getLabel());
        }

        Map interface_type = new HashMap<>();
        for (SysDict interfaceTypeSysDict : interfaceTypeSysDicts) {
            interface_type.put(interfaceTypeSysDict.getValue(), interfaceTypeSysDict.getLabel());
        }

        Map report_type = new HashMap<>();
        for (SysDict reportTypeSysDict : reportTypeSysDicts) {
            report_type.put(reportTypeSysDict.getValue(), reportTypeSysDict.getLabel());
        }

        Map theme = new HashMap<>();
        for (SysDict themeSysDict : themeSysDicts) {
            theme.put(themeSysDict.getValue(), themeSysDict.getLabel());
        }

        Map order_type = new HashMap<>();
        for (SysDict orderTypeSysDict : orderTypeSysDicts) {
            order_type.put(orderTypeSysDict.getValue(), orderTypeSysDict.getLabel());
        }

        // 创建一个空的 dictsList 容器
        List<Map<String, String>> dictsList = new ArrayList<>();
        dictsList.add(table_type);
        dictsList.add(interface_type);
        dictsList.add(report_type);
        dictsList.add(theme);
        dictsList.add(order_type);

        List<String> btnList = new ArrayList();
        btnList.add("*");

        //返回数据
        UserDataVo vo = UserDataVo.builder()
                .userInfo(userInfo)
                .menuList(menuList)
                .dictsList(dictsList)
                .btnList(btnList)
                .build();

        return Result.success(vo);
    }

    /**
     * 主页统计信息
     *
     * @return
     */
    @Override
    public Result baseInfo() {

        //封装user
        //查询用户总数
        Long result = yddUserMapper.selectCount(null);
        Integer total = result.intValue();
        //查询男用户数量   0是男，1是女
        QueryWrapper<YddUser> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(YddUser::getSex, 0);
        Long countMan = yddUserMapper.selectCount(queryWrapper);
        Integer user_man = countMan.intValue();
        //查询男用户数量   0是男，1是女
        // 清除之前设置的查询条件
        queryWrapper.clear();
        queryWrapper.lambda().eq(YddUser::getSex, 1);
        Long countWoman = yddUserMapper.selectCount(queryWrapper);
        Integer user_woman = countWoman.intValue();

        user user = new user();
        user.setTotal(total);
        user.setUser_man(user_man);
        user.setUser_woman(user_woman);


        //封装hot_article (List)热门文章数据
        LambdaQueryWrapper<YddArticle> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.orderByDesc(YddArticle::getClick)
                .last("limit 7");
//        Page<YddArticle> page = new Page<>(1, 7);//获取前面7条数据
        List<YddArticle> recordeList = yddArticleMapper.selectList(lambdaQueryWrapper);
        //当前页数据
//        List<YddArticle> recordeList = page.getRecords();
        List<HotArticle> hot_article = new ArrayList<>();

        for (YddArticle yddArticle : recordeList) {
            HotArticle hotArticle = new HotArticle();
            BeanUtils.copyProperties(yddArticle, hotArticle);
            hot_article.add(hotArticle);
        }


        //最新用户数据 new_user
        LambdaQueryWrapper<YddUser> yddUserLambdaQueryWrapper = Wrappers.lambdaQuery();
        yddUserLambdaQueryWrapper.orderByDesc(YddUser::getId).last("limit 7");
        //Page<YddUser> yddUserPage = new Page<>(1, 7);//获取前面7条数据
        //Page<YddUser> userPage = yddUserMapper.selectPage(yddUserPage, yddUserLambdaQueryWrapper);
        List<YddUser> yddUsers = yddUserMapper.selectList(yddUserLambdaQueryWrapper);

        //创建new_user封装数据
        List<NewUser> new_user = new ArrayList<>();
        //获取当前页
        //<YddUser> yddUserList = userPage.getRecords();
        for (YddUser yddUser : yddUsers) {
            NewUser newUser = new NewUser();
            BeanUtils.copyProperties(yddUser, newUser);
            new_user.add(newUser);
        }


        //获取数据细心总数统计结果
        //获取广告位总数
        Long advPosCount = yddAdvposMapper.selectCount(null);
        Integer advpos_num = advPosCount.intValue();

        //获取广告图总数
        Long advImgCount = yddAdvimgMapper.selectCount(null);
        Integer advimg_num = advImgCount.intValue();

        //获取标签总数
        Long tagCount = yddTagMapper.selectCount(null);
        Integer tag_num = tagCount.intValue();

        //获取管理员总数
        Long sysUserCount = sysUserMapper.selectCount(null);
        int admin_num = sysUserCount.intValue();

        //获取文章总数
        Long articleCount = yddArticleMapper.selectCount(null);
        int article_num = articleCount.intValue();


        //获取分类总数
        Long CateCount = yddCateMapper.selectCount(null);
        int cate_num = CateCount.intValue();


        //获取所有分类数据
        //创建new_user封装数据
        List<AllCate> all_cate = new ArrayList<>();
        List<YddCate> yddCateList = yddCateMapper.selectList(null);
        LambdaQueryWrapper<YddArticle> articleLambdaQueryWrapper = Wrappers.lambdaQuery();
        for (YddCate yddCate : yddCateList) {
            //封装进自定义实体类
            AllCate allCate = new AllCate();
            allCate.setId(yddCate.getId());
            allCate.setCatename(yddCate.getCatename());
            //根据分类id查询分类下的文章数量
            articleLambdaQueryWrapper.clear();
            articleLambdaQueryWrapper.eq(YddArticle::getCateid, yddCate.getId());
            int num = yddArticleMapper.selectCount(articleLambdaQueryWrapper).intValue();
            //封装进实体类
            allCate.setNum(num);
            //封装到List集合中
            all_cate.add(allCate);
        }

        //添加数据到vo中
        BaseInfoVo vo = BaseInfoVo.builder()
                .all_cate(all_cate)
                .advpos_num(advpos_num)
                .tag_num(tag_num)
                .admin_num(admin_num)
                .article_num(article_num)
                .cate_num(cate_num)
                .advimg_num(advimg_num)
                .new_user(new_user)
                .hot_article(hot_article)
                .user(user)
                .build();


        return Result.success(vo);
    }

}





